package com.slz3.business.module.socialManagement.socLife.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.socialManagement.socLife.domain.SocLife;
import com.slz3.business.module.socialManagement.socLife.domain.SocLifeFactory;
import com.slz3.business.module.socialManagement.socLife.steward.SocLifeSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

@Slf4j
@Service
public class SocLifeServiceImpl implements SocLifeService {

    @Override
    public SocLife save(SocLife socLife) {
        return Repositorys.socLife.save(SocLifeFactory.initBaseFields(socLife));
    }

    @Override
    public Page<SocLife> page(MultiValueMap<String, String> params) {
        Page<SocLife> socLifePage = Repositorys.socLife.findAll(
                SocLifeSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return socLifePage;
    }

    @Override
    public SocLife modify(SocLife socLifeNew) {
        return Repositorys.socLife.saveAndFlush(SocLifeFactory.initBaseFields(socLifeNew));
    }

    @Override
    public Optional<SocLife> single(UUID uuid) {
        return Repositorys.socLife.findById(uuid);
    }

    @Override
    public List<SocLife> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.socLife.findAllById(uuids);
    }

    @Override
    public List<SocLife> delete(List<SocLife> socLifeList) {
        socLifeList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            SocLifeFactory.initBaseFields(item);
        });
        return Services.socLifeTransactional.batchModify(socLifeList);
    }

}
